package com.canaan.common.encrypt;

import cn.hutool.json.JSONUtil;
import com.canaan.common.core.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * EDI接口
 *
 * @author mingliang1.qi
 * @since 2025/2/14
 */
@Slf4j
@RestController
@RequestMapping("/edi")
public class EdiController {

    /**
     * 持久化需求单的topic
     */
    public static final String TOPIC = "global-tms-requirement_order-create";

    /**
     * 需求单管理新增
     *
     * @param request 请求参数
     * @return 新增结果
     */
    @PostMapping(value = "/createRequirementOrder")
    public String add(HttpServletRequest request) {
        String requirementOrderReqJsonStr = getCommonReq(request).getData();
        log.info("requirementOrderReqJsonStr解析后内容为：{}", request.getParameter("cid"));
        try {
           return buildResult(true, null);
        } catch (Exception e) {
            log.error("创建需求单异常,msg:{}", e.getMessage(), e);
            return buildResult(false, e.getMessage());
        }
    }

    /**
     * 生成返回结果
     *
     * @param hasSuccess
     * @return
     */
    private static String buildResult(boolean hasSuccess, String defaultMsg) {
        ApiResult<ResultDTO<AjaxResult>> result = new ApiResult<>();
        ResultDTO<AjaxResult> resultDTO = new ResultDTO<>();
        AjaxResult ajaxResult = new AjaxResult();
        if (hasSuccess) {
            resultDTO.setStatus("success");
            ajaxResult.success();
        } else {
            resultDTO.setStatus("fail");
            ajaxResult.success();
        }
        result.setContent(resultDTO);
        resultDTO.setData(ajaxResult);

        return AesCryptorUtils.encrypt(JSONUtil.toJsonStr(result), AesCryptorUtils.SECURITY_KEY);
    }

    /**
     * 获取公共请求
     *
     * @param request
     * @return
     */
    private BaseReqParam getCommonReq(HttpServletRequest request) {
        if (request instanceof HttpRequestDecodeFilter.DecodeHttpServletRequestWrapper) {
            String requestBody = ((HttpRequestDecodeFilter.DecodeHttpServletRequestWrapper) request).getRequestBody();
            BaseReqParam baseReq = JSONUtil.toBean(requestBody, BaseReqParam.class);
            baseReq.setRequestSource(request.getParameter("cid"));
            return baseReq;
        }

//        if (request instanceof RequestWrapper) {
//            ServletRequest servletRequest = ((RequestWrapper) request).getRequest();
//            if (servletRequest instanceof HttpRequestDecodeFilter.DecodeHttpServletRequestWrapper) {
//                String requestBody = ((HttpRequestDecodeFilter.DecodeHttpServletRequestWrapper) servletRequest).getRequestBody();
//                BaseReqParam baseReq = JSON.parseObject(requestBody, BaseReqParam.class);
//                baseReq.setRequestSource(servletRequest.getParameter("cid"));
//                return baseReq;
//            }
//        }

        return null;
    }
}
